// error.cpp
#include <windows.h>
#include <stdio.h>

void ErrorMessage(char* szMessage, HRESULT hr)
{
    if(HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
		hr = HRESULT_CODE(hr);

	char* szError;
	if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&szError, 0, NULL) != 0)
	{
		printf("%s: %s", szMessage, szError);
		LocalFree(szError);
	}
	else
		printf("Error number not found\n");
}

void main(int argc, char** argv)
{
	if(argc == 2)
	{
		HRESULT hr;
		sscanf(argv[1], "%x", &hr);
		printf("HRESULT = %x\n", hr);
		ErrorMessage("Error message", hr);
	}
	else
		printf("Sample usage:\nerror 80070057\n");
}